
 import { useEditorStore } from "@/stores/editStore"
 import { storeToRefs } from "pinia"
const editorStore = useEditorStore() 
const {editorInstance} = storeToRefs(editorStore)
const loadHeadings = () => {
   const headings = [] as any[]
          if (!editorInstance.value) return
          const transaction = editorInstance.value.state.tr
          if (!transaction) return
          editorInstance.value?.state.doc.descendants((node:any, pos:any) => {
            if (node.type.name === 'heading') {
              console.log(pos, node)
              const start = pos
              const end = pos + node.content.size
              // const end = pos + node
              const id = `heading-${headings.length + 1}`
              if (node.attrs.id !== id) {
                transaction?.setNodeMarkup(pos, undefined, {
                  ...node.attrs,
                  id
                })
              }
            // 收集标题信息
              headings.push({
                level: node.attrs.level,
                text: node.textContent,
                start,
                end,
                id
              })
            }
          })

          transaction?.setMeta('addToHistory', false)
          transaction?.setMeta('preventUpdate', true)
          editorInstance.value.view.dispatch(transaction);
          // editor.value?.view.dispatch(transaction)
          editorStore.setHeadings(headings)
    }

    

export {loadHeadings}    